package andytest;

import static org.junit.Assert.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import uk.ac.glasgow.demosocs.Election;
import uk.ac.glasgow.demosocs.ElectionState;
import uk.ac.glasgow.demosocs.impl.OneOfNVoteImpl;
import uk.ac.glasgow.demosocs.impl.VotingSystemStub;

public class votingsystemTest {
	VotingSystemStub votingsys ;
	SimpleDateFormat format ;

	@Before
	public void setUp() throws Exception {
		votingsys = new VotingSystemStub();
		format = new SimpleDateFormat("yyyy/MM/dd");
		votingsys.setCurrentUser("321", "password2");
		votingsys.createElection("Ellection1", "type1");

	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test() throws ParseException {
		System.out.println(votingsys.getCurrentUserGUID());
		assertSame(votingsys.getCurrentUserGUID(),"321");
		
		
		Date openNomi = format.parse("2012/02/11");
		Date startElec = format.parse("2012/02/20");
		Date endElec = format.parse("2012/02/25");
		assertSame(votingsys.getElectionImplByID("1").getElectionState(), ElectionState.NOT_STARTED);
		
		votingsys.setCurrentUser("321", "password2");
		votingsys.editElection(openNomi, startElec, endElec, "electorate1");
		assertSame(votingsys.getElectionImplByID("1").getElectionState(), ElectionState.NOMINATIONS_OPEN);

		for(Election e : votingsys.getElections()){
			System.out.println(e.getEID());
			System.out.println(e.getElectionState());
			System.out.println(e.getOpenNominations());
			System.out.println(e.getStart());
			System.out.println(e.getEnd());
		}
		//nominate process current user is 321
		votingsys.nominateCandidate("8888");
		votingsys.setCurrentUser("8888", "password5");
		votingsys.acceptNomination();
		assertTrue(votingsys.getElectionImplByID("1").isCandidateSetComtainsGUID("8888"));
		
	}
	@Test
	public void testVoting() throws ParseException{

		System.out.println(votingsys.getCurrentUserGUID());
		assertSame(votingsys.getCurrentUserGUID(),"321");
		
		
		Date openNomi = format.parse("2012/02/11");
		Date startElec = format.parse("2012/02/20");
		Date endElec = format.parse("2012/02/25");
		
		votingsys.setCurrentUser("321", "password2");
		votingsys.editElection(openNomi, startElec, endElec, "electorate1");
		
		for(Election e : votingsys.getElections()){
			System.out.println(e.getEID());
			System.out.println(e.getElectionState());
			System.out.println(e.getOpenNominations());
			System.out.println(e.getStart());
			System.out.println(e.getEnd());
		}
		
		//nominate process current user is 321
		votingsys.nominateCandidate("8888");
		votingsys.setCurrentUser("8888", "password5");
		votingsys.acceptNomination();
		
		//nominate another one 7777
		votingsys.nominateCandidate("7777");
		votingsys.setCurrentUser("7777", "password6");
		votingsys.acceptNomination();
		
		assertTrue(votingsys.getElectionImplByID("1").isCandidateSetComtainsGUID("8888"));
		assertTrue(votingsys.getElectionImplByID("1").isCandidateSetComtainsGUID("7777"));

		//date for voting

		 openNomi = format.parse("2011/02/11");
		 startElec = format.parse("2011/02/20");
		 endElec = format.parse("2012/02/25");
		
		votingsys.setCurrentUser("321", "password2");
		votingsys.setCurrentElection("1");
		
		votingsys.editElection(openNomi, startElec, endElec, "123123");
		votingsys.setCurrentUser("321", "password2");
		votingsys.castVote(new OneOfNVoteImpl("8888"));
		System.out.println("####"+votingsys.getElectionImplByID("1").getCandidateByGUID("8888").getVotes());
		
		votingsys.setCurrentUser("123", "password1");
		votingsys.castVote(new OneOfNVoteImpl("7777"));

		 openNomi = format.parse("2011/02/11");
		 startElec = format.parse("2011/02/20");
		 endElec = format.parse("2011/02/25");
		 votingsys.editElection(openNomi, startElec, endElec, "sdfefs");
		 
		 votingsys.setCurrentUser("321", "password2");
		 votingsys.publishResults();
		
	}

}
